<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    chartData: {
      type: Object,
      default: () => ({
        xAxis: ['文档创建', '文档编辑', '文档分享', '文档审批', '文档归档'],
        series: [
          {
            name: '数量',
            data: [120, 200, 150, 80, 70]
          }
        ]
      })
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler() {
        this.updateChart()
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.updateChart()
    },
    updateChart() {
      if (!this.chart) return
      
      const option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          },
          textStyle: {
            fontSize: 14,
            color: '#fff'
          },
          backgroundColor: 'rgba(0, 0, 0, 0.8)',
          borderColor: 'rgba(255, 255, 255, 0.2)',
          borderWidth: 1
        },
        legend: {
          data: this.chartData.series.map(item => item.name),
          textStyle: {
            fontSize: 12,
            color: '#fff',
            fontWeight: 'bold'
          },
          itemWidth: 12,
          itemHeight: 12,
          itemGap: 15
        },
        grid: {
          top: 40, // 增加顶部空间
          left: '3%',
          right: '3%',
          bottom: '8%', // 增加底部空间
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          data: this.chartData.xAxis,
          axisTick: {
            alignWithLabel: true,
            show: false // 隐藏刻度线
          },
          axisLabel: {
            fontSize: 12,
            color: '#fff',
            fontWeight: 'bold',
            interval: 0, // 显示所有标签
            rotate: 0 // 不旋转标签
          },
          axisLine: {
            lineStyle: {
              color: 'rgba(255, 255, 255, 0.3)'
            }
          }
        }],
        yAxis: [{
          type: 'value',
          axisTick: {
            show: false
          },
          axisLabel: {
            fontSize: 12,
            color: '#fff',
            fontWeight: 'bold'
          },
          axisLine: {
            lineStyle: {
              color: 'rgba(255, 255, 255, 0.3)'
            }
          },
          splitLine: {
            lineStyle: {
              color: 'rgba(255, 255, 255, 0.1)'
            }
          }
        }],
        series: this.chartData.series.map(item => ({
          name: item.name,
          type: 'bar',
          barWidth: '50%', // 减小柱宽，为文字留出更多空间
          data: item.data,
          itemStyle: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              { offset: 0, color: '#83bff6' },
              { offset: 0.5, color: '#188df0' },
              { offset: 1, color: '#188df0' }
            ]),
            borderRadius: [4, 4, 0, 0] // 添加圆角
          },
          label: {
            show: true,
            position: 'top',
            fontSize: 12,
            color: '#fff',
            fontWeight: 'bold'
          },
          animationDuration
        }))
      }
      
      this.chart.setOption(option)
    }
  }
}
</script>
